package cn.dckeji.dcssm.controller;


import cn.dckeji.dcssm.common.model.ResultModel;
import cn.dckeji.dcssm.common.utils.MD5Util;
import cn.dckeji.dcssm.entity.Admin;
import cn.dckeji.dcssm.entity.Role;
import cn.dckeji.dcssm.service.AdminService;
import cn.dckeji.dcssm.service.RoleService;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 后台管理员表 前端控制器
 * </p>
 *
 * @author Kenny
 * @since 2019-02-21
 */
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;
    @Autowired
    private RoleService roleService;

    /**
     * 后台登录
     * @param userName
     * @param password
     * @param session
     * @return
     */
    @PostMapping("/login")
    public ResultModel login(String userName, String password, HttpSession session) {
        Admin admin = adminService.getOne(new QueryWrapper<Admin>().eq("login_name", userName));
        if (admin == null) {
            return ResultModel.failure("登录失败,账号或者密码不正确");
        }
        String adminLoginPassword = admin.getLoginPassword();
        String s = MD5Util.MD5Encode(password, "utf-8");
        if (s.equals(adminLoginPassword)) {
            //登录成功
            session.setAttribute("remark", admin.getRemark());
            session.setAttribute("adminId",admin.getId());
            return ResultModel.success();
        }
        return ResultModel.failure("登录失败,账号或者密码不正确");
    }

    /**
     * 修改密码
     * @param oldpassword
     * @param newpassword
     */
    @PostMapping(value = "/changePassword")
    public ResultModel changePassword(String oldpassword, String newpassword,HttpSession session){

        String adminId= (String) session.getAttribute("adminId");

        Admin admin = adminService.getById(adminId);

        if (admin.getLoginPassword().equals(MD5Util.MD5Encode(oldpassword,"utf-8"))) {
            admin.setLoginPassword(MD5Util.MD5Encode(newpassword,"utf-8"));
            adminService.updateLoginPassword(admin);
            return ResultModel.success();
        }
        return ResultModel.failure("原密码不正确");
    }

    /**
     * 分页查询管理员
     * @param pageNumber
     * @param pageSize
     * @param reamrk
     * @return
     */
    @GetMapping("/page")
    public JSONObject page(Integer pageNumber, Integer pageSize, @RequestParam(value = "remark",required = false) String reamrk) {
        QueryWrapper<Admin> adminQueryWrapper = new QueryWrapper<>();
        adminQueryWrapper.orderByDesc("gmt_create");
        if (StringUtils.isNotBlank(reamrk)) {
            adminQueryWrapper.like("remark", reamrk);
        }
        IPage<Admin> page = adminService.page(new Page<>(pageNumber, pageSize), adminQueryWrapper);
        List<Admin> records = page.getRecords();

        List<Map> adminMapList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(records)) {
            for (Admin admin:records) {
                Map<String, Object> stringObjectMap = BeanUtil.beanToMap(admin);
                String roleId = admin.getRoleId();
                Role role = roleService.getById(roleId);
                String roleName = role.getRoleName();
                stringObjectMap.put("roleName", roleName);
                adminMapList.add(stringObjectMap);
            }
        }
        return ResultModel.page(page.getTotal(), adminMapList);
    }

    /**
     * 重置管理员密码
     * @param adminId
     * @return
     */
    @PostMapping("/restPassword")
    public ResultModel<Object> restPassword(String adminId) {
        adminService.updateAdminPassword(adminId);
        return ResultModel.success();
    }

    /**
     * 删除管理员
     * @param adminId
     * @return
     */
    @PostMapping("/deleteById")
    public ResultModel<Object> deleteById(String adminId) {
        adminService.removeById(adminId);
        return ResultModel.success();
    }

    /**
     * 保存管理员
     * @param admin
     * @return
     */
    @PostMapping("/saveAdmin")
    public ResultModel<Object> saveAdmin(Admin admin) {
        boolean b = adminService.saveAdmin(admin);
        if (b) {
            return ResultModel.success();
        }else {
            return ResultModel.failure("登录名已存在,请更换重试");
        }
    }
}

